package com.ts.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.ErrorProjectDetailEntity;
import com.ts.proj.entity.ErrorprojectEntity;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.ErrorprojectDetailService;
import com.ts.proj.service.ErrorprojectService;
import com.ts.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//说明 字符串：Test，作为替换字符串   异常项目和否决

@Controller
public class ErrorprojectController extends BaseController {

    @Autowired
    private ErrorprojectDetailService errorprojectDetailService;

    @Autowired
    private ErrorprojectService errorprojectService;

    @RequestMapping("/business-errorProject-list")
    public ModelAndView providerList(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            viewName = "business/errorProject/errorProject-list";
            modelAndView.addObject("userId", systemUserEntity.getId());
        }

        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/business-vetoProject-list")
    public ModelAndView vetoList(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            viewName = "business/vetoProject/vetoProject-list";
            modelAndView.addObject("userId", systemUserEntity.getId());
        }

        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/error-detail-add/{id}")
    public ModelAndView projectContractAdd(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            modelAndView.addObject("id", id);
            modelAndView.addObject("localId", -1);
            modelAndView.addObject("error", null);
            viewName = "business/errorProject/error-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/error-detail-edit/{id}")
    public ModelAndView projectMeetAddEdit(@PathVariable("id") Integer id){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            ErrorProjectDetailEntity errorProjectDetailEntity = errorprojectDetailService.findById(id);
            ErrorprojectEntity errorprojectEntity = errorprojectService.findById(Integer.valueOf(errorProjectDetailEntity.getProID()));
            modelAndView.addObject("errorprojectId", errorprojectEntity.getId());//异常项目id
            modelAndView.addObject("error", errorProjectDetailEntity);
            viewName = "business/errorProject/error-edit";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    //id : uuid   desc: detailid:detailid
    @RequestMapping("/errorProject-upload/{id}/{desc}/{detailId}")
    public ModelAndView projectMeetUpload(@PathVariable("id") String id,@PathVariable("desc") String desc,
                                          @PathVariable("detailId") Integer detailId){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            PageData pageData = new PageData();
            pageData.put("deleteFlag","N");
            pageData.put("uuid",id);
            ErrorprojectEntity errorprojectEntity = errorprojectService.findAll(pageData).get(0);
            ErrorProjectDetailEntity detailServiceById = errorprojectDetailService.findById(detailId);
            modelAndView.addObject("id", errorprojectEntity.getProjectId());
            modelAndView.addObject("fileName", desc);
            modelAndView.addObject("detail",detailServiceById);
            viewName = "business/errorProject/errorProject-upload";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }


    @RequestMapping("/vetoProject-upload/{id}/{desc}/{detailId}")
    public ModelAndView projectVetoUpload(@PathVariable("id") String id,@PathVariable("desc") String desc,
                                          @PathVariable("detailId") Integer detailId){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            PageData pageData = new PageData();
            pageData.put("deleteFlag","N");
            pageData.put("uuid",id);
            ErrorprojectEntity errorprojectEntity = errorprojectService.findAll(pageData).get(0);
            ErrorProjectDetailEntity detailServiceById = errorprojectDetailService.findById(detailId);
            modelAndView.addObject("id", errorprojectEntity.getProjectId());
            modelAndView.addObject("fileName", desc);
            modelAndView.addObject("detail",detailServiceById);
            viewName = "business/vetoProject/vetoProject-upload";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}